/**
 * 
 */
package com.eej.act.obj;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author jlumietu
 *
 */
public class TaldeakParser extends DefaultHandler {

	String temp;
	String anterior = null;
	String tagAnterior;
	String tag;
	boolean firstArray = false;
	boolean secondArray = false;
	ArrayList<String> remeros = null;
	Taldea talde = null;
	int iTalde = 0;
	ListaTaldea lisTalde = new ListaTaldea();
	boolean finEquipo = false;
	
	boolean anteriorEsKey = false;
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub
		temp += new String(ch,start,length);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		// TODO Auto-generated method stub
		//System.out.println(temp+"</"+qName+">");
		//System.out.print(temp + "</"+qName+">");
		if(qName.equals("string")){
			//System.out.print("anterior: " +  anterior + " -> " + temp);
			if(this.anterior.equals("Arraunlariak")){
				// Si tenemos secondArray activado, entonces estamos leyendo los integrantes de la plantilla
				// cada string es un campo de la informacion del remero.
				this.remeros.add(temp);
			}else if(anterior.equals("Grupo")){
				this.talde.setGrupo(temp);
			}else if(anterior.equals("Izena")){
				this.talde.setIzena(temp);
			}else if(anterior.equals("Helbidea1")){
				this.talde.setHelbidea1(temp);
			}else if(anterior.equals("Helbidea2")){
				this.talde.setHelbidea2(temp);
			}else if(anterior.equals("Helbidea3")){
				this.talde.setHelbidea3(temp);
			}else if(anterior.equals("Telefonoa")){
				this.talde.setTelefonoa(temp);
			}else if(anterior.equals("Emaila")){
				this.talde.setEmaila(temp);
			}else if(anterior.equals("Argazkia")){
				this.talde.setArgazkia(temp);
			}else if(anterior.equals("Logo")){
				this.talde.setLogo(temp);
			}else if(anterior.equals("Web")){
				this.talde.setWeb(temp);
			}
		}else if(qName.equals("key")){
			this.anteriorEsKey = true;
			this.anterior = temp;
			if(this.tagAnterior.equals("dict") || finEquipo){
				try{
					this.iTalde = Integer.parseInt(temp);
				}catch(NumberFormatException nfe){
					this.anterior = temp;
				}
			}
		}else if(qName.equals("array")){
			//System.out.print("anterior: " +  anterior + " -> " + temp + "secondArray=" + this.secondArray);
			if(this.secondArray){
				secondArray = false;
				Arraunlaria remero = new Arraunlaria();
				remero.setFoto(this.remeros.get(0));
				remero.setNombre(this.remeros.get(1));
				remero.setApellido(this.remeros.get(2));
				remero.setNacimiento(this.remeros.get(3));
				remero.setLugar(this.remeros.get(4));
				remero.setAntiguedad(this.remeros.get(5));
				remero.setProcedencia(this.remeros.get(6));
				this.talde.getArraunlariak().add(remero);
			}else if(this.firstArray){
				firstArray = false;
			} 
		}else if(qName.equals("dict")){
			finEquipo = true;
			this.lisTalde.put(this.iTalde, this.talde);
		}
		
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		//super.startElement(uri, localName, qName, attributes);
		temp = "";
		//System.out.print("\n<"+qName+">");
		if(anterior!=null && anterior.equals("Arraunlariak")){
			if(qName.equals("array")){
				if(firstArray){
					secondArray = true;
					this.remeros = new ArrayList<String>();
				}else{
					firstArray = true;
				}
			}
		}else if(qName.equals("dict")){
			this.finEquipo = false;
			this.talde = new Taldea();
		}
		tagAnterior = tag;
		tag = qName;
	}
	
	public static void main(String args[]) throws Exception{
		SAXParserFactory spf = SAXParserFactory.newInstance();
		spf.setValidating(false);
		SAXParser sp = spf.newSAXParser();
		String file = Thread.currentThread().getContextClassLoader().getResource("taldeak_let.plist").getFile();
		System.out.println("file: " + file);
		InputStream is = 
			new FileInputStream(file);
		TaldeakParser tp = new TaldeakParser();
		sp.parse(is, tp);
		System.out.println(tp.lisTalde.getStringRepresentation());
	}
	
}
